23. Maven Properties

Maven Properties

ND079 JPND C3 L2 A19 Maven Properties

Assigning Properties

In the pom generated by archetype:generate, there are two properties set in a <properties> block.

  <properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

These set the value maven.compiler.source to 1.7. This value is used by the maven compiler plugin, and you could do the same thing by configuring the plugin directly.

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>

Changing these values will change the Java version used by the compiler plugin.

Creating and Using Properties

You can define your own properties and reference them elsewhere in your pom.

<properties>
  <some.plugin.version>2.0</some.plugin.version>
<properties>

The above statement defines a property named some.plugin.version. You can now reference it with the same name:

<plugin>
  <groupId>my.group</groupId>
  <artifactId>some-plugin</artifactId>
  <version>${some.plugin.version}</version>
</plugin>

Automatic Properties

There are 4 other types of properties Maven creates automatically.

  • Environment variables: any variables in the shell's environment can be referenced with ${env.VAR_NAME}, for example ${env.PATH}.
  • POM elements: Properties in the pom can be referenced according to their place in the object structure. For example, if I wanted to reference the <project><groupId>value</groupId></project>, I could use ${project.groupId}
  • Settings.xml: Users can provide customizations to their maven profile in a file called settings.xml. These variables can be referenced with ${settings.propName}
  • Java System properties: Anything provided by java.lang.System.getProperties() can be accessed using ${java.propName}